<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="d" uri="http://dgg.net/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title></title>
    <c:import url="../../header.jsp"/>
</head>
<body>
<div class="content-warp">
    <div class="dgg-list-top-warp">
        <div class="screen-search">
            <ul>
                <li>
                    <label>关键字：</label>
                    <input type="text" class="dggui-input dggui-input-large" id="searchKey" name="searchKey" placeholder="名称/编号" style="width: 190px;">
                </li>
                <li>
                    <button class="dggui-btn" onclick="tableReload();">搜索</button>
                    <button class="dggui-btn dggui-btn-primary" onclick="cleanSearch()">清空条件</button>
                </li>
            </ul>
        </div>
    </div>
    <div class="list-content">
        <div class="tool-bar">
            <div class="fl">
                <a href="javascript:void(0)" class="dggui-btn" onclick="addPaper()">新增试题</a>
                <a href="javascript:void(0)" class="dggui-btn" onclick="importExcel()">导入试题</a>
            </div>
        </div>
        <div class="table-warp">
            <table class="dggui-table" id="testPaper">

            </table>
        </div>
    </div>
</div>

<c:import url="/WEB-INF/jsp/footer.jsp"/>
<script>
    var tables;
    dggui.use(['table'], function (){
        var table = dggui.table;
        var opt={
            "elem":"#testPaper",
            "ajax": sysInfo.basePath +"/rzsc/evaluating/paper_list.do",
            "fnServerParams": function (aoData) {
                //搜索条件
                aoData = $.extend(aoData, {
                    //关键字
                    searchKey : $('#searchKey').val()
                });
                return aoData;
            },
            "aoColumns": [
                {
                    "title": "序号",
                    "orderable": false,
                    "render": function(data, type, full, meta) {
                        return meta.row + 1;
                    }
                },
                {
                    'title': '编号',
                    "orderable": false,
                    "data": "paperCode",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    'title': '名称',
                    "orderable": false,
                    "data": "paperName",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    'title': '基础分值',
                    "orderable": false,
                    "data": "paperScore",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    'title': '试卷类型',
                    "orderable": false,
                    "data": "paperType",
                    "render": function (data, type, full, meta) {
                        if(data==1){
                            data = "不按照题目顺序填写"
                        }
                        if(data==2){
                            data = "按照题目顺序答题"
                        }
                        return data;
                    }
                },
                {
                    'title': '最新更新时间',
                    "orderable": false,
                    "data": "updateTime",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    'title': '最后更新人',
                    "orderable": false,
                    "data": "updaterName",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    'title': '操作',
                    "orderable": false,
                    "render": function(data, type, row, meta) {
                        var htm = '<a href="javascript:addPaper(\''+row.id+'\')" class="dggui-link">修改</a>' +
                            '&nbsp;&nbsp;<a href="javascript:editPaperSubject(\''+row.id+'\',\''+row.paperName+'\')" class="dggui-link">编辑题目</a>' +
                            '&nbsp;&nbsp;<a href="javascript:deletePaper(\''+row.id+'\')" class="dggui-link">删除</a>';
                        return htm;
                    }
                }
            ]
        };
        tables = table.render(opt);
        // 处理dataTable 异常错误信息
        $.fn.dataTable.ext.errMode = 'none';//不抛出异常
        tables.on( 'error.dt', function ( e, settings, techNote, message ) {
            if(checkEmpty(settings.jqXHR.responseJSON.error)){dggui.dialog.errors(settings.jqXHR.responseJSON.error, 2000);}
        });
    });
    /**
     * 新增选项
     */
    function addPaper(paperId){
        var title = "";
        if(isEmpty(paperId)){
            title = '新增试题';
        }else{
            title = '编辑试题';
        }
        dggui.dialog.loading();
        $.ajax({
            url: sysInfo.basePath+"/rzsc/evaluating/add_paper.html",
            data:{id:paperId},
            dataType:'html',
            success:function (res){
                dggui.dialog.loading('close');
                dggui.dialog.open({
                    title: title,
                    content: res,
                    area: '500px',
                    yes: function (index) {
                        var paperName = $("#paperName").val();
                        var paperCode = $("#paperCode").val();
                        var paperScore = $("#paperScore").val();
                        var paperType = $("#paperType").val();
                        if(isEmpty(paperName)){
                            dggui.dialog.tip('名称不能为空！',$('#paperName'));
                            return;
                        }
                        if(isEmpty(paperCode)){
                            dggui.dialog.tip('编号不能为空！',$('#paperCode'));
                            return;
                        }
                        if(isEmpty(paperScore)){
                            dggui.dialog.tip('基础分值不能为空！',$('#paperScore'));
                            return;
                        }
                        if(isEmpty(paperType)){
                            dggui.dialog.tip('试卷类型不能为空！',$('#paperType'));
                            return;
                        }
                        dggui.dialog.loading();
                        var evaluatingBasicPaper = serializeObject($("#addPaperForm"));
                        $.ajax({
                            url: sysInfo.basePath + "/rzsc/evaluating/save_paper.do",
                            data: evaluatingBasicPaper,
                            dataType : "JSON",
                            success: function (res) {
                                dggui.dialog.loading('close');
                                if (res.code == 0) {
                                    dggui.dialog.succeed(res.msg);
                                    layer.close(index);
                                    tableReload();
                                } else {
                                    dggui.dialog.errors(res.msg);
                                }
                            }
                        });
                    }
                })
            }
        });
    }





    var paperHtml;
    /**
     *导入试题
     *
     */
    function importExcel(){
        $.ajax({
            url: sysInfo.basePath+"/rzsc/evaluating/add_paper_excel.html",
            dataType:'html',
            success:function (res){
                paperHtml = dggui.dialog.open({
                    title: "导入试题",
                    content: res,
                    area: '500px',
                    btn:[],
                    yes: function (index) {

                    },
                    success:function () {
                        dggui.use('upload', function () {
                            var upload = dggui.upload, uploadObj;
                            upload.render({
                                elem: '#excelFile'
                                , url: sysInfo.basePath + '/rzsc/evaluating/import_excel.do'
                                , multiple: false
                                , accept: 'file' //允许上传的文件类型
                                , size: 20 * 1024 //最大允许上传的文件大小
                                ,auto:false
                                ,bindAction: '#uploadExcel'
                                , before: function (obj) {
                                    uploadObj = obj;
                                    dgg.dialog.loading();
                                }
                                , done: function (res) {
                                    debugger
                                    dgg.dialog.loading('close');
                                    if (res.code == 0) {
                                        dgg.dialog.succeed("上传成功");
                                        layer.close(paperHtml);
                                    } else {
                                        dgg.dialog.errors(res.msg);
                                        layer.close(paperHtml);
                                    }
                                }
                            });
                        })
                    }
                });
            }
        });
    }





    /**
     * 删除渠道
     * @param id
     */
    function deletePaper(id) {
        dggui.dialog.confire('确认执行此操作？', '操作提示', function () {
            dggui.dialog.loading();
            $.ajax({
                url: sysInfo.basePath + "/rzsc/evaluating/delete_paper.do",
                data: {id: id},
                success: function (res) {
                    dggui.dialog.loading('close');
                    if (res.code == 0) {
                        dggui.dialog.succeed(res.msg);
                        tableReload();
                    } else {
                        dggui.dialog.errors(res.msg);
                    }
                }
            });

        })
    }
    
    //编辑题目
    function editPaperSubject(paperId,paperName) {
        var url = sysInfo.basePath + "/rzsc/evaluating/edit_paper_subject.html?paperId=" + paperId;
        window.parent.updateTab("编辑题目:"+paperName, url);
    }
    //列表刷新
    function tableReload() {
        tables.ajax.reload();
    }
</script>
</body>
</html>